<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>快速上手 | 微观世界</title>
    <meta name="generator" content="VuePress 1.5.0">
    <link rel="icon" href="/logo.jpg">
    <link rel="manifest" href="/manifest.json">
    <meta name="description" content="计算机软件 + 物联网 + 云计算项目开发、设计、咨询">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
    <meta name="msapplication-TileColor" content="#000000">
    <link rel="preload" href="/assets/css/0.styles.55f00579.css" as="style"><link rel="preload" href="/assets/js/app.92627f89.js" as="script"><link rel="preload" href="/assets/js/2.817fcd47.js" as="script"><link rel="preload" href="/assets/js/18.74f9e612.js" as="script"><link rel="prefetch" href="/assets/js/10.791ab255.js"><link rel="prefetch" href="/assets/js/11.1dc01403.js"><link rel="prefetch" href="/assets/js/12.c421b677.js"><link rel="prefetch" href="/assets/js/13.8fa15714.js"><link rel="prefetch" href="/assets/js/14.2cca4cf4.js"><link rel="prefetch" href="/assets/js/15.e9f5696c.js"><link rel="prefetch" href="/assets/js/16.62cab094.js"><link rel="prefetch" href="/assets/js/17.6c789b79.js"><link rel="prefetch" href="/assets/js/19.98ca7c32.js"><link rel="prefetch" href="/assets/js/20.5052a859.js"><link rel="prefetch" href="/assets/js/21.3ea5972a.js"><link rel="prefetch" href="/assets/js/22.18d546b5.js"><link rel="prefetch" href="/assets/js/23.069cf840.js"><link rel="prefetch" href="/assets/js/3.62c9d964.js"><link rel="prefetch" href="/assets/js/4.58e718d2.js"><link rel="prefetch" href="/assets/js/5.addf51f6.js"><link rel="prefetch" href="/assets/js/6.a0e5b372.js"><link rel="prefetch" href="/assets/js/7.5d4ccb0f.js"><link rel="prefetch" href="/assets/js/8.ad529520.js"><link rel="prefetch" href="/assets/js/9.cc29df21.js">
    <link rel="stylesheet" href="/assets/css/0.styles.55f00579.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">微观世界</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="成功案例" class="dropdown-title"><span class="title">成功案例</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/product/smart-home/" class="nav-link">
  智能家居
</a></li><li class="dropdown-item"><!----> <a href="/product/share-ark/" class="nav-link">
  共享快递柜
</a></li><li class="dropdown-item"><!----> <a href="/product/share-washing/" class="nav-link">
  共享洗衣机
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开源项目" class="dropdown-title"><span class="title">开源项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/ws-cloud/gateway/" class="nav-link router-link-active">
  ws-cloud-gateway
</a></li></ul></div></div><div class="nav-item"><a href="/contact/" class="nav-link">
  联系我们
</a></div><div class="nav-item"><a href="/about/" class="nav-link">
  关于我们
</a></div><div class="nav-item"><a href="/link/" class="nav-link">
  友情链接
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="成功案例" class="dropdown-title"><span class="title">成功案例</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/product/smart-home/" class="nav-link">
  智能家居
</a></li><li class="dropdown-item"><!----> <a href="/product/share-ark/" class="nav-link">
  共享快递柜
</a></li><li class="dropdown-item"><!----> <a href="/product/share-washing/" class="nav-link">
  共享洗衣机
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开源项目" class="dropdown-title"><span class="title">开源项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/ws-cloud/gateway/" class="nav-link router-link-active">
  ws-cloud-gateway
</a></li></ul></div></div><div class="nav-item"><a href="/contact/" class="nav-link">
  联系我们
</a></div><div class="nav-item"><a href="/about/" class="nav-link">
  关于我们
</a></div><div class="nav-item"><a href="/link/" class="nav-link">
  友情链接
</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span></span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/ws-cloud/gateway/0.x/" class="active sidebar-link">快速上手</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/ws-cloud/gateway/0.x/#快速上手" class="sidebar-link">快速上手</a></li><li class="sidebar-sub-header"><a href="/ws-cloud/gateway/0.x/#请求转发规则" class="sidebar-link">请求转发规则</a></li><li class="sidebar-sub-header"><a href="/ws-cloud/gateway/0.x/#源码解析" class="sidebar-link">源码解析</a></li></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h2 id="快速上手"><a href="#快速上手" class="header-anchor">#</a> 快速上手</h2> <h3 id="安装openresty"><a href="#安装openresty" class="header-anchor">#</a> 安装openresty</h3> <p><a href="http://openresty.org/cn/installation.html" target="_blank" rel="noopener noreferrer">http://openresty.org/cn/installation.html<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <h3 id="启动服务"><a href="#启动服务" class="header-anchor">#</a> 启动服务</h3> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">git</span> clone https://github.com/tech-microworld/ws-cloud-gateway.git
<span class="token builtin class-name">cd</span> resty-gateway
<span class="token function">git</span> checkout v0.1 -b v0.1
<span class="token comment"># -g 'daemon off;是在前台运行，如果想后台运行可以去掉</span>
openresty -p <span class="token variable"><span class="token variable">`</span><span class="token builtin class-name">pwd</span><span class="token variable">`</span></span> -c conf/nginx.conf -g <span class="token string">'daemon off;'</span>
</code></pre></div><h3 id="向etcd注册节点信息"><a href="#向etcd注册节点信息" class="header-anchor">#</a> 向etcd注册节点信息</h3> <blockquote><p>当然实际情况是在服务启动的时候自动注册的，这里为了测试，手动模拟的服务注册过程</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token comment"># 注册user服务</span>
etcdctl put /micros/service/user/127.0.0.1:1024 <span class="token number">1</span>
etcdctl put /micros/service/user/127.0.0.1:1025 <span class="token number">1</span>

<span class="token comment"># 注册auth服务</span>
etcdctl put /micros/service/auth/127.0.0.1:1026 <span class="token number">1</span>
etcdctl put /micros/service/auth/127.0.0.1:1027 <span class="token number">1</span>
</code></pre></div><h3 id="发送http请求"><a href="#发送http请求" class="header-anchor">#</a> 发送http请求</h3> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token comment"># 测试auth服务</span>
<span class="token function">curl</span> http://localhost:10000/open/api/auth/login
<span class="token function">curl</span> http://localhost:10000/inner/api/auth/has-login

<span class="token comment"># 测试user服务</span>
<span class="token function">curl</span> http://localhost:10000/inner/api/user/fav
<span class="token function">curl</span> http://localhost:10000/inner/api/user/info
</code></pre></div><h2 id="请求转发规则"><a href="#请求转发规则" class="header-anchor">#</a> 请求转发规则</h2> <p>0.x 版本约定了url转发规则，修改路由前缀需要修改配置文件 <code>lualib/my/config.lua</code> 的<code>open_api_prefix</code> 和 <code>inner_api_prefix</code> 两个参数</p> <div class="language-lua extra-class"><pre class="language-lua"><code><span class="token keyword">local</span> config <span class="token operator">=</span> <span class="token punctuation">{</span>
    test <span class="token operator">=</span> <span class="token punctuation">{</span>
        appName <span class="token operator">=</span> <span class="token string">&quot;resty-gateway&quot;</span><span class="token punctuation">,</span>
        discovery <span class="token operator">=</span> <span class="token punctuation">{</span>
            etcd <span class="token operator">=</span> <span class="token punctuation">{</span>
                http_host <span class="token operator">=</span> <span class="token string">&quot;http://192.168.1.102:2379&quot;</span><span class="token punctuation">,</span>
                etcdctl_api <span class="token operator">=</span> <span class="token string">&quot;3&quot;</span><span class="token punctuation">,</span>
                api_prefix <span class="token operator">=</span> <span class="token string">&quot;/v3&quot;</span><span class="token punctuation">,</span>
                service_register_path <span class="token operator">=</span> <span class="token string">&quot;/micros/service&quot;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            open_api_prefix <span class="token operator">=</span> <span class="token string">&quot;/open/api&quot;</span><span class="token punctuation">,</span>
            inner_api_prefix <span class="token operator">=</span> <span class="token string">&quot;/inner/api&quot;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    prod <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><ol><li>对外api前缀：<code>/open/api/service_name/**</code>，后端服务路由：<code>/open/api/**</code></li> <li>对内api前缀：<code>/inner/api/service_name/**</code>，后端服务路由：<code>/**</code></li></ol> <p>例如<code>/open/api/user/fav</code>会转发到user服务的<code>/open/api/fav</code>接口；<code>/inner/api/user/info</code>会转发到user服务的<code>/info</code>接口。通过url规范我们就能很轻易的做api鉴权，而不需要将对外api和对内api部署成2个服务（当然也要考虑实际情况，是否符合实际项目要求）。</p> <h2 id="源码解析"><a href="#源码解析" class="header-anchor">#</a> 源码解析</h2> <ul><li><p>init_by_lua_file &quot;lua/init.lua&quot;</p> <p>初始化项目配置</p></li> <li><p>init_worker_by_lua_file &quot;lua/init_worker.lua&quot;</p> <p>初始化服务发现组件，监听服务节点变更信息</p></li> <li><p>rewrite_by_lua_file 'lua/rewrite.lua'</p> <ol><li>生成requestId，方便链路跟踪和问题排查</li> <li>解析请求服务名称</li> <li>重写后端请求url，去掉服务名</li></ol></li> <li><p>access_by_lua_file 'lua/gateway.lua'</p> <p>通过服务发现组件查询服务节点信息</p></li> <li><p>balancer_by_lua_file 'lua/server_rr.lua'</p> <p>通过查询到的节点信息，轮训其中一个节点，将请求转发到后端服务</p></li> <li><p>user.conf</p> <p>模拟用户服务</p></li> <li><p>auth.conf</p> <p>模拟认证服务</p></li></ul></div> <footer class="page-edit"><!----> <!----></footer> <!----> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.92627f89.js" defer></script><script src="/assets/js/2.817fcd47.js" defer></script><script src="/assets/js/18.74f9e612.js" defer></script>
  </body>
</html>
